package org.railage.server.services;

import java.util.Arrays;
import java.util.List;

import org.railage.common.dto.StationOfRouteDTO;
import org.railage.common.protocol.Result;
import org.railage.server.dao.StationDAO;
import org.railage.server.dao.StationDAOImpl;
import org.railage.server.dao.StationOfRouteDAO;
import org.railage.server.dao.StationOfRouteDAOImpl;
import org.railage.server.entities.Station;
import org.railage.server.entities.StationOfRoute;
import org.railage.server.util.HibernateUtil;

public class StationService {
	
	private StationDAO stationDAO = new StationDAOImpl();
	private StationOfRouteDAO stationOfRouteDAO = new StationOfRouteDAOImpl();

	public Result addStation(Object obj) {
		String stationName = (String) obj;
		int resultCode = stationDAO.addStation(stationName);
		
		Result result = null;
		switch (resultCode) {
		case 0:
			result = new Result(true, "Station " + stationName + " added");
			break;
		case 1:
			result = new Result(false, "Station " + stationName + " already exists!");
			break;
		case -1:
			result = new Result(false, HibernateUtil.DB_SESSION_ERROR_MSG);
			break;
		default:
			break;
		}
		return result;
	}

	public Result deleteStation(Object obj) {
		String stationName = (String) obj;
		int resultCode = stationDAO.deleteStation(stationName);
		
		Result result = null;
		switch (resultCode) {
		case 0:
			result = new Result(true, "Station " + stationName + " deleted");
			break;
		case 1:
			result = new Result(false, "Station " + stationName + " does not exist");
			break;
		case 2:
			result = new Result(false, "Station " + stationName + " is on route. Cannot delete!");
			break;
		case -1:
			result = new Result(false, HibernateUtil.DB_SESSION_ERROR_MSG);
			break;
		default:
			break;
		}
		return result;
	}

	public StationOfRouteDTO[] getOrderedStationsOfRoute(Object obj) {
		List<StationOfRoute> stationsOfRoute = stationOfRouteDAO
				.getOrderedStationsOfRoute((String) obj);
		StationOfRouteDTO[] stationsOfRouteDTO = new StationOfRouteDTO[stationsOfRoute
				.size()];
		int i = 0;
		for (StationOfRoute stationOfRoute : stationsOfRoute) {
			stationsOfRouteDTO[i] = stationOfRoute.toDTO();
			stationsOfRouteDTO[i].setOrder(i + 1);
			i++;
		}
		return stationsOfRouteDTO;
	}

	public String[] getOrderedStationsOfTrip(Object obj) {
		int tripID = (Integer) obj;
		List<Station> stations = stationDAO.getOrderedStationsOfTrip(tripID);
		if (stations.size() == 0) {
			return null;
		} else {
			String[] stationsArr = new String[stations.size()];
			int i = 0;
			for (Station station : stations) {
				stationsArr[i] = station.getName();
				i++;
			}
			return stationsArr;
		}
	}

	public String[] getStationNames(Object obj) {
		List<Station> stations = stationDAO.getAllStations();
		String[] stationNames = new String[stations.size()];
		int i = 0;
		for (Station station : stations) {
			stationNames[i] = station.getName();
			i++;
		}
		Arrays.sort(stationNames);
		return stationNames;
	}
	

}
